<?php
namespace app\common\controller;

/**
 * 事项及抄送数据源类
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/10/16
 * Time: 16:30
 */
class Source{

    /**
     * 获取来源信息
     * @param $sourceid
     * @param $source 1：巡店；2：内部协同；3：推广工作；4：工作日志；5：活动开展；6：通知公告
     * @return int
     */
    public function get_source($sourceid,$source){
        $source_info = [];
        switch($source){
            case 1:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $crm_detail = model('cp_region_modular')->get_detail($maps,'id,cp_floorid,is_qualified,describe,pic,c_time');
                $crm_detail['pic'] = !empty($crm_detail['pic']) ? json_decode($crm_detail['pic'],true) : [];

                //查找发起人
                $maps = [];
                $maps[] = ['id','=',$crm_detail['cp_floorid']];
                $cf_detail = model('cp_floor')->get_detail($maps,'id,openid');
                $maps = [];
                $maps[] = ['openid','=',$cf_detail['openid']];
                $u_detail = model('user')->get_detail($maps,'id,username');

                $source_info['source_username'] = $u_detail['username'];
                $source_info['source_is_qualified'] = $crm_detail['is_qualified'];
                $source_info['source_describe'] = $crm_detail['describe'];
                $source_info['source_pic'] = $crm_detail['pic'];
                $source_info['source_c_time'] = $crm_detail['c_time'];
                break;
            case 2:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wrx_detail = model('work_record_xt')->get_detail($maps,'id,openid,content,pic,c_time');
                $wrx_detail['pic'] = !empty($wrx_detail['pic']) ? json_decode($wrx_detail['pic'],true) : [];

                //查找发起人
                $maps = [];
                $maps[] = ['openid','=',$wrx_detail['openid']];
                $u_detail = model('user')->get_detail($maps,'id,username');

                //查找事项
                $maps = [];
                $maps[] = ['sourceid','=',$sourceid];
                $maps[] = ['source','=',$source];
                $m_detail =model('matter')->get_detail($maps,'id,out_time');

                $source_info['source_username'] = $u_detail['username'];
                $source_info['source_content'] = $wrx_detail['content'];
                $source_info['source_out_time'] = $m_detail['out_time'];
                $source_info['source_pic'] = $wrx_detail['pic'];
                $source_info['source_c_time'] = $wrx_detail['c_time'];
                break;
            case 3:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wre_detail = model('work_record_extend')->get_detail($maps,'id,openid,name,content,pic,c_time');
                $wre_detail['pic'] = !empty($wre_detail['pic']) ? json_decode($wre_detail['pic'],true) : [];

                $source_info['source_name'] = $wre_detail['name'];
                $source_info['source_content'] = $wre_detail['content'];
                $source_info['source_pic'] = $wre_detail['pic'];
                $source_info['source_c_time'] = $wre_detail['c_time'];
                break;
            case 4:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wrd_detail = model('work_record_daily')->get_detail($maps,'id,openid,name,content,pic,c_time');
                $wrd_detail['pic'] = !empty($wrd_detail['pic']) ? json_decode($wrd_detail['pic'],true) : [];

                $source_info['source_name'] = $wrd_detail['name'];
                $source_info['source_content'] = $wrd_detail['content'];
                $source_info['source_pic'] = $wrd_detail['pic'];
                $source_info['source_c_time'] = $wrd_detail['c_time'];
                break;
            case 5:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wra_detail = model('work_record_active')->get_detail($maps,'id,openid,name,content,person_num,pic,c_time');
                $wra_detail['pic'] = !empty($wra_detail['pic']) ? json_decode($wra_detail['pic'],true) : [];

                $source_info['source_name'] = $wra_detail['name'];
                $source_info['source_content'] = $wra_detail['content'];
                $source_info['source_person_num'] = $wra_detail['person_num'];
                $source_info['source_pic'] = $wra_detail['pic'];
                $source_info['source_c_time'] = $wra_detail['c_time'];
                break;
            case 6:
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wrn_detail = model('work_record_notice')->get_detail($maps,'id,openid,name,content,pic,c_time');
                $wrn_detail['pic'] = !empty($wrn_detail['pic']) ? json_decode($wrn_detail['pic'],true) : [];

                $source_info['source_name'] = $wrn_detail['name'];
                $source_info['source_content'] = $wrn_detail['content'];
                $source_info['source_pic'] = $wrn_detail['pic'];
                $source_info['source_c_time'] = $wrn_detail['c_time'];
                break;
            default:
                return 0;
        }

        $data['source_info'] = $source_info;
        return $data;
    }
}